﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Drawing;


namespace MallInteractiveMap.CustomControl
{
    public class MIMGridView : GridView
    {
        public MIMGridView()
        {
            this.Sorting += new GridViewSortEventHandler(HRPortalGridView_Sorting);
            PageSize = 10;
            AutoGenerateColumns = false;
        }

        void HRPortalGridView_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (ViewState["SortDirection"] == null)
            {
                ViewState["SortDirection"] = "DESC";
            }
            string sortDir = Convert.ToString(ViewState["SortDirection"]);
            if (sortDir.Equals("ASC"))
            {
                ViewState["SortDirection"] = "DESC";
            }
            else
            {
                ViewState["SortDirection"] = "ASC";
            }
        }


        /// <summary>
        /// returns sort direction. 
        /// </summary>
        protected string GridSortDirection
        {
            get
            {
                return ViewState["SortDirection"] != null ? Convert.ToString(ViewState["SortDirection"]) : "ASC";

            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), this.ClientID + "ADDScript", GetScript("#" + this.ClientID), true);
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
        }

        public bool IsRowSelectOnClick
        {
            set
            {
                ViewState["RowSelectOnClick"] = value;
            }
            get
            {
                return ViewState["RowSelectOnClick"] == null ? true : Convert.ToBoolean(ViewState["RowSelectOnClick"]);
            }
        }

        protected override void OnRowCreated(GridViewRowEventArgs e)
        {
            base.OnRowCreated(e);
            if (e.Row.RowType == DataControlRowType.DataRow && IsRowSelectOnClick)
            {
                e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackEventReference(this, "Select$" + e.Row.RowIndex);
            }
            else if (e.Row.RowType == DataControlRowType.Header)
            {
                AddImageToHeader(e.Row);
            }
        }

        public bool IsLastColumnNotSortable { get; set; }

        private void AddImageToHeader(GridViewRow e)
        {
            string imageUrl = "~/images/UpArrow.gif";
            if (this.GridSortDirection.Equals("ASC"))
            {
                imageUrl = "~/images/DownArrow.gif";
            }
            int count = 0;
            foreach (TableCell cell in e.Cells)
            {
                count++;
                System.Web.UI.WebControls.Image image = new System.Web.UI.WebControls.Image();
                image.ImageUrl = imageUrl;
                image.ImageAlign = ImageAlign.AbsMiddle;
                cell.Controls.Add(image);

                if (IsLastColumnNotSortable && e.Cells.Count == count && cell.Controls.Count > 0 && (LinkButton)cell.Controls[0] != null)
                {
                    Label lbl = new Label();
                    lbl.Text = ((LinkButton)cell.Controls[0]).Text;
                    cell.Controls.Clear();
                    cell.Controls.Add(lbl);

                }
            }
        }



        private string GetScript(string grdID)
        {
            string removeCss = string.Empty;
            if (this.AllowPaging)
            {
                removeCss = "$(\"" + grdID + " tr:eq(" + (this.PageSize + 1).ToString() + ")\").removeClass();"
                                   + "$(\"" + grdID + " tr:eq(" + (this.PageSize + 2).ToString() + ")\").removeClass();";
            }
            string gvscript = @"$(document).ready(function() {"
                                   + "$(\"" + grdID + " tr:even\").addClass(\"eventr\");"
                                   + "$(\"" + grdID + " tr:odd\").addClass(\"oddtr\");"
                                   + " $(\"" + grdID + " tr:eq(0) th\").addClass(\"trHeader\");"
                                   + removeCss
                                   + " $(\"" + grdID + " tr\").mouseover(function() {"
                                   + " if ($(this).hasClass(\"oddtr\")) {"
                                   + "    $(this).addClass(\"troverOdd\");"
                                   + "    $(this).removeClass(\"oddtr\");"
                                   + "}"
                                   + " if ($(this).hasClass(\"eventr\")) {"
                                   + "    $(this).removeClass(\"eventr\");"
                                   + "    $(this).addClass(\"troverEven\");"
                                   + "}"
                                       + " }).mouseout(function() {"
                                       + " if ($(this).hasClass(\"troverEven\")) {"
                                       + "    $(this).removeClass(\"troverEven\");"
                                       + "$(this).addClass(\"eventr\");"
                                       + "    } else if ($(this).hasClass(\"troverOdd\")) {"
                                       + "    $(this).removeClass(\"troverOdd\");"
                                       + "  $(this).addClass(\"oddtr\");"
                                       + "}"
                                        + "});"
                                   + "});";
            return gvscript;
        }
    }
}
